<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="工程编号" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          clearable
          placeholder="请输入工程编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleCalculate"
        >试算</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="success"
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :cell-style="tableRowClassName" v-loading="loading" :data="repairUnitList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="小区" prop="areaName"/>
      <el-table-column align="center" label="楼宇" prop="groupName"/>
      <el-table-column align="center" label="单元" prop="unitName"/>
      <el-table-column align="center" label="户" prop="doorName"/>
      <el-table-column align="center" label="房屋编号" prop="houseId"/>
      <el-table-column align="center" label="面积" prop="space"/>
      <el-table-column align="center" label="预缴金额" prop="budgetMoney"/>
      <el-table-column align="center" label="账户余额" prop="balance"/>
      <el-table-column align="center" label="分摊金额" prop="repairMoney"/>
      <el-table-column align="center" label="不足金额" prop="noMoney"/>
      <el-table-column align="center" label="拨付金额" prop="payMoney"/>
      <el-table-column align="center" label="分摊后余额" prop="afterBalance"/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改户资金修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工程编号" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="工程造价" prop="projectMoney">
          <el-input v-model="form.projectMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="试算单价" prop="price">
          <el-input v-model="form.price" placeholder=""/>
        </el-form-item>
        <el-form-item label="核算造价" prop="trueMoney">
          <el-input v-model="form.trueMoney" placeholder=""/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="trialCalculateForm">试算</el-button>
        <el-button type="primary" @click="checkCalculateForm">核算</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="titleDetail" :visible.sync="openDetail" append-to-body width="500px">
      <el-form ref="formDetail" :model="formDetail" :rules="rules" label-width="80px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="formDetail.houseId" placeholder="" :disabled="true"/>
        </el-form-item>
        <el-form-item label="分摊金额" prop="repairMoney">
          <el-input v-model="formDetail.repairMoney" placeholder=""/>
        </el-form-item>
        <el-form-item label="不足金额" prop="noMoney" >
          <el-input v-model="formDetail.noMoney" placeholder="" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listRepairUnit, getRepairUnit, delRepairUnit,getCalculate,updateCalculate,updateRepairUnit} from "@/api/repair/repairUnit";

export default {
  name: "RepairUnit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户资金修改表格数据
      repairUnitList: [],
      // 弹出层标题
      title: "",
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        projectId: null,
        projectSts:0,
        projectType:1,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      formDetail: {},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "工程编号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({row}){
      return (row.balance<=row.budgetMoney*0.3?'color: red;':"")
    },
    /** 查询户资金修改列表 */
    getList() {
      this.loading = true;
      listRepairUnit(this.queryParams).then(response => {
        this.repairUnitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
      this.resetDetail();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        budgetMoney: null,
        repairMoney: null,
        noMoney:null,
        payMoney:null,
        afterBalance: null,
        balance: null
      };
    },
    resetDetail() {
      this.formDetail = {
        projectId: null,
        houseId: null,
        repairMoney: null,
        noMoney: null
      };
      this.resetForm("formDetail");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.houseId = selection.map(item => item.houseId)
      this.projectId = selection.map(item => item.projectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleCalculate() {
      this.reset();
      getCalculate(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.resetDetail();
      const houseId = this.houseId
      const projectId = this.projectId
      getRepairUnit(houseId,projectId).then(response => {
        this.formDetail = response.data;
        this.openDetail = true;
        this.titleDetail = "修改";
      });
    },
    /** 试算 */
    trialCalculateForm() {
      getCalculate(this.form).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "试算";
      });
    },
    /** 核算 */
    checkCalculateForm() {
      updateCalculate(this.form).then(response => {
        this.$modal.msgSuccess("核算成功");
        this.open = false;
        this.getList();
      });
    },

    /** 修改 */
    submitForm() {
      updateRepairUnit(this.formDetail).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openDetail = false;
        this.getList();
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const doorIds = row.doorId || this.ids;
      this.$modal.confirm('是否确认删除户资金修改编号为"' + doorIds + '"的数据项？').then(function () {
        return delRepairUnit(doorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('repair/repairUnit/export', {
        ...this.queryParams
      }, `repairUnit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
